﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Koonsoft.CustomerService.Entity;
using System.Data;

namespace Koonsoft.CustomerService.DAL
{
    public class IssueStateUpdateLogDAL
    {
        private Database database = DatabaseFactory.CreateDatabase("CustomerService");
        public bool Create(IssueStateUpdateLog issuelog, out string msg)
        {
            msg = "";
            string sql = @"insert into [IssueStateUpdateLog]([IssueID],[OldState],[NewState],[UserName],[Mark],[Time],[LogType])
                            values (@IssueID,@OldState,@NewState,@UserName,@Mark,@Time,@LogType)";

             try
            {
                using (var cmd = database.GetSqlStringCommand(sql))
                {
                    database.AddInParameter(cmd, "@IssueID", DbType.Guid , issuelog.IssueID);
                    database.AddInParameter(cmd, "@Mark", DbType.String, issuelog.Mark);
                    database.AddInParameter(cmd, "@NewState", DbType.String, issuelog.NewState);
                    database.AddInParameter(cmd, "@OldState", DbType.String, issuelog.OldState);
                    database.AddInParameter(cmd, "@UserName", DbType.String, issuelog.UserName);
                    database.AddInParameter(cmd, "@Time", DbType.DateTime, DateTime.Now);
                    database.AddInParameter(cmd, "@LogType", DbType.Int32, issuelog.LogType);
                    return database.ExecuteNonQuery(cmd) > 0;
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message + ";" + ex.StackTrace;
            }
             return false;

        }

        public List<IssueStateUpdateLog> GetLog(Guid guid)
        {
            string sql = @" select IssueID,UserName,[Time],Mark ,LogType
                            ,(select [State] from wf where WFID = a.OldState) as 'OldState'
                            ,(select [State] from wf where WFID = a.NewState) as 'NewState'
                            from  [IssueStateUpdateLog] a where [IssueID] =@IssueID";

            List<IssueStateUpdateLog> Logs = new List<IssueStateUpdateLog>();
            try
            {
                using (var cmd = database.GetSqlStringCommand(sql))
                {
                    database.AddInParameter(cmd, "@IssueID", DbType.Guid, guid);

                    DataTable dt = database.ExecuteDataSet(cmd).Tables[0];

                    foreach (DataRow dr in dt.Rows)
                    {
                        IssueStateUpdateLog log = new IssueStateUpdateLog();
                        log.IssueID = guid;
                        log.Mark = dr["Mark"].ToString();
                        log.NewState = dr["NewState"].ToString();
                        log.OldState = dr["OldState"].ToString();
                        log.UserName = dr["UserName"].ToString();
                        log.Time = DateTime.Parse(dr["Time"].ToString());

                        Logs.Add(log);
                    }

                    return Logs;
                }
            }
            catch (Exception ex)
            {
            }
            return new List<IssueStateUpdateLog>();
        }
    }
}
